﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BarTender;
using Synergy.dbmodel;
namespace Synergy
{

    public partial class FormRunCard : Form
    {



        int i = 0;
        int ksrow;
        int jsrow;
        public FormRunCard()
        {
            InitializeComponent();
        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button_search_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("001001"))
            {
                ListMO();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }

        }
        /// <summary>
        /// 
        /// </summary>
        private void ListMO()
        {
            string BeginDate;
            string EndDate;
            BeginDate = this.dateTimePicker_ksrq.Value.ToString("yyyy-MM-dd");
            EndDate = this.dateTimePicker_jsrq.Value.ToString("yyyy-MM-dd");
            string sql;
            sql = @"select    '' 行号,  a.MoId 生产订单号ID, a.MoCode 生产订单号, a.CreateDate 制单日期 ,'' 开工日期,
'' 完工日期 
FROM      mom_order a   where 1=1 ";
            if (this.textBox加工单号.Text != "")
            {
                sql += " and a.MoCode like '%" + textBox加工单号.Text + "%' ";
            }
           
            if (this.dateTimePicker_ksrq.Checked)
            {
                sql += " and a.CreateDate>='" + BeginDate + "' ";
            }
            if (this.dateTimePicker_jsrq.Checked)
            {
                sql += " and a.CreateDate<='" + EndDate + "'";
            }
            DataTable tb = pub.U8GetTable(sql);
            if (tb != null)
            {
                for (int i = 0; i < tb.Rows.Count; i++)
                {
                    tb.Rows[i]["行号"] = (i + 1).ToString();
                    tb.Rows[i]["开工日期"] = pub.Get开工日期by生产订单号ID(tb.Rows[i]["生产订单号ID"].ToString());
                    tb.Rows[i]["完工日期"] = pub.Get完工日期by生产订单号ID(tb.Rows[i]["生产订单号ID"].ToString());

                }
            }

            //this.dataGridView1
            this.dataGridView1.AutoGenerateColumns = false;
            this.dataGridView1.DataSource = tb;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            this.dataGridView1.Refresh();

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                string 单据编号 = dataGridView1.Rows[i].Cells["C生产订单号"].Value.ToString();

                string sqlbh = " select * from Z_printrecord where 单据编号='" + 单据编号 + "' ";

                DataTable tbbh = pub.U8GetTable(sqlbh);
                if (tbbh != null)
                {
                    if (tbbh.Rows.Count > 0)
                    {
                        dataGridView1.Rows[i].Cells["C生产订单号"].Style.BackColor = Color.Red;
                    }
                }

                //if (dataGridView1.Rows[i].Cells["C生产订单号"].Value.ToString() == "")
                //{
                //    string khbhmc;
                //    khbhmc = dataGridView1.Rows[i].Cells["Cxiaohuobianhao"].Value.ToString() + "," + dataGridView1.Rows[i].Cells["Hdiaorubumen"].Value.ToString();
                //    MessageBox.Show("有：" + khbhmc + "没有门店对照！", "提示", MessageBoxButtons.OK);
                //    dataGridView1.Rows[i].Cells["HT6kehumingheng"].Style.BackColor = Color.Red;
                //}
            }
        }

        private void button_print_Click(object sender, EventArgs e)
        {
            if (pub.是否有权限("001002"))
            {
                BarTender.Application ba = new BarTender.Application();
                BarTender.Format bfmt;
                bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\汇总流转卡.btw");

                bfmt.SetNamedSubStringValue("二维1", "12321312312313");
                if (this.printDialog1.ShowDialog() == DialogResult.OK)
                {

                    bfmt.PrintOut();
                }
                ba.Quit();
            }
            else
            {
                MessageBox.Show("您未得到该授权", "提示", MessageBoxButtons.OK);
            }
        }

        private void FormRunCard_Load(object sender, EventArgs e)
        {
            this.dataGridView1.AutoGenerateColumns = false;
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;

            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox2.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            this.progressBar1.Value = 0;
            if (ksrow > jsrow)
            {
                int r;
                r = ksrow;
                ksrow = jsrow;
                jsrow = r;

            }
            for (int i = ksrow; i <= jsrow; i++)
            {
                this.dataGridView1.Rows[i].Cells[1].Value = checkBox1.Checked;
                if (this.progressBar1.Value >= 99)
                {
                    this.progressBar1.Value = 0;
                }
                else
                {
                    this.progressBar1.Value++;
                }
            }
            this.progressBar1.Value = 100;

        }

        private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
        {
            ksrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            jsrow = e.RowIndex;
            this.checkBox1.Checked = false;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

            int hang;
            int lie;
            hang = e.RowIndex;
            lie = e.ColumnIndex;
            string 生产订单号ID;
            string 生产订单号;
            int pages = 1;//打印张数
            if (hang < 0)
            {
                return;
            }
            //查看

            if (this.dataGridView1.Columns[lie].Name == "C查看")//查看
            {
                ///查看明细
                生产订单号ID = this.dataGridView1.Rows[hang].Cells["C生产订单号ID"].Value.ToString();
                生产订单号 = this.dataGridView1.Rows[hang].Cells["C生产订单号"].Value.ToString();
                DataTable dtb = ListDetail(生产订单号ID);
                this.dataGridView2.AutoGenerateColumns = false;
                this.dataGridView2.DataSource = dtb;

                this.dataGridView2.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
                this.dataGridView2.Refresh();

                for (int i = 0; i < dtb.Rows.Count; i++)
                {
                    string 生产订单号1 = this.dataGridView1.Rows[hang].Cells["C生产订单号"].Value.ToString();
                    string 行号 = dtb.Rows[i]["行号"].ToString();

                    string sqlbh = " select * from Z_printrecord where 单据编号='" + 生产订单号1 + "' and 生产订单行='" + 行号 + "' ";

                    DataTable tbbh = pub.U8GetTable(sqlbh);
                    if (tbbh != null)
                    {
                        if (tbbh.Rows.Count > 0)
                        {
                            dataGridView2.Rows[i].Cells["D行号"].Style.BackColor = Color.Red;
                        }
                    }

                    //if (dataGridView1.Rows[i].Cells["C生产订单号"].Value.ToString() == "")
                    //{
                    //    string khbhmc;
                    //    khbhmc = dataGridView1.Rows[i].Cells["Cxiaohuobianhao"].Value.ToString() + "," + dataGridView1.Rows[i].Cells["Hdiaorubumen"].Value.ToString();
                    //    MessageBox.Show("有：" + khbhmc + "没有门店对照！", "提示", MessageBoxButtons.OK);
                    //    dataGridView1.Rows[i].Cells["HT6kehumingheng"].Style.BackColor = Color.Red;
                    //}
                }

            }
            else if (this.dataGridView1.Columns[lie].Name == "C打印")//打印汇总
            {

                if (this.printDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    pages = this.printDialog1.PrinterSettings.Copies;
                    //this.printDialog1.PrinterSettings.PrinterName
                    生产订单号ID = this.dataGridView1.Rows[hang].Cells["C生产订单号ID"].Value.ToString();
                    PrintRunCard(生产订单号ID, pages);
                    MessageBox.Show("打印完成", "提示", MessageBoxButtons.OK);
                }
            }
            else if (this.dataGridView1.Columns[lie].Name == "C打印流转卡")//打印工序流转卡
            {
                if (this.printDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    pages = this.printDialog1.PrinterSettings.Copies;
                    //this.printDialog1.PrinterSettings.PrinterName
                    生产订单号ID = this.dataGridView1.Rows[hang].Cells["C生产订单号ID"].Value.ToString();
                    if (PrintRunCardDetail(生产订单号ID, pages))
                    {
                        MessageBox.Show("打印完成", "提示", MessageBoxButtons.OK);
                    }
                    else
                    {
                        MessageBox.Show("打印失败", "提示", MessageBoxButtons.OK);
                    }
                }
            }


        }
        /// <summary>
        /// 打印明细流转卡  当先用到的唯一打印流转卡按钮
        /// </summary>
        /// <param name="生产订单号ID"></param>
        /// <param name="pages"></param>
        private bool PrintRunCardDetail(string 生产订单号ID, int pages)
        {
            ///
            int 每页行数 = 10;
            int 总页数 = 1;
            int 当前页码 = 1;
            int 当前行 = 1;
            int 总行数 = 0;
            string 二维码;
            bool result = false;
            BarTender.Application ba = new BarTender.Application();
            BarTender.Format bfmt;
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\工序流转卡两列.btw");
            string sql;
            sql = @" SELECT  distinct 工艺路线ID,生产订单号,生产订单行号 FROM      View_工序明细 where 生产订单号ID='" + 生产订单号ID + "' order by 生产订单行号 desc,工艺路线ID  ";
            DataTable tb1 = pub.U8GetTable(sql);
            //如果有两行工艺路线，就跳出选择哪一个工艺路线
            if (tb1 != null && tb1.Rows.Count > 1)
            {
                //所有的工艺路线ID 以逗号分隔
                List<string> 工艺路线IDs = new List<string>();
                for (int i = 0; i < tb1.Rows.Count; i++)
                {
                    工艺路线IDs.Add(tb1.Rows[i]["工艺路线ID"].ToString() );
                }
                Form选择工艺路线 formSelect = new Form选择工艺路线();
                formSelect.PRoutingIds = string.Join("','", 工艺路线IDs);
                formSelect.ShowDialog();
                if (formSelect.DialogResult == DialogResult.OK)
                {
                    //从tb1找出工艺路线id对应的行并赋值给一个同样的table
                    tb1 = tb1.Select("工艺路线ID='" + formSelect.工艺路线ID + "'").CopyToDataTable();
                }
                else
                {
                    return false;
                }
            }
            ///总共有多少条工艺路线。
            if (tb1 != null)
            {
                for (int i = 0; i < tb1.Rows.Count; i++)
                {
                    删除工单(tb1.Rows[0]["生产订单号"].ToString(), tb1.Rows[i]["生产订单行号"].ToString());

                    result = true;
                    sql = @" SELECT   生产订单号, 生产订单号ID, 工艺路线ID, 版本代码, 数量, 物料名称, 物料规格, 物料编号, 加工顺序, 工序名称, 
                工序编号ID, 计量单位名称, 计量单位编号, 工序编号, 生产订单行号, 制单日期,是否倒冲,是否委外,是否检验,生产订单行ID,inventory.cinvaddcode 存货代码
       FROM      View_工序明细 join inventory on  View_工序明细.物料编号=inventory.cinvcode where 生产订单号ID='" + 生产订单号ID + "' and 生产订单行号 = '"+ tb1.Rows[i]["生产订单行号"].ToString() + "'  and 工艺路线ID='" + tb1.Rows[i]["工艺路线ID"].ToString() + "' order by 加工顺序 ";
                   

                    DataTable tb2 = pub.U8GetTable(sql);

                    if (tb2 != null)
                    {
                        


                        string sql生产订单行数 = $@"select count(1) from View_生产订单明细 where 生产订单号 = '{tb2.Rows[0]["生产订单号"].ToString()}'";
                        string 生产订单行数 = SqlSugarHelp.scope.Ado.GetString(sql生产订单行数);

                        string sql行备注 = $@"select Remark from  mom_orderdetail where MoId = {生产订单号ID} and SortSeq = '{tb2.Rows[0]["生产订单行号"].ToString()}'";
                        string 行备注 = SqlSugarHelp.scope.Ado.GetString(sql行备注);

                        string sqlcus = $@"select Define2 from mom_order where MoId = {生产订单号ID}";
                        string 客户 = SqlSugarHelp.scope.Ado.GetString(sqlcus);

                        string sql业务员 = $@"select Define3 from mom_order where MoId = {生产订单号ID}";
                        string 业务员 = SqlSugarHelp.scope.Ado.GetString(sql业务员);

                        string 工艺路线ID = tb2.Rows[0]["工艺路线ID"].ToString();
                        //取 行号 前面的
                        int index = 行备注.IndexOf("行号");
                        if (index != -1)
                        {
                            行备注 = 行备注.Substring(0, index - 2);
                        }

                        bfmt.SetNamedSubStringValue("生产订单号", tb2.Rows[0]["生产订单号"].ToString());
                        bfmt.SetNamedSubStringValue("存货代码", tb2.Rows[0]["存货代码"].ToString());
                        bfmt.SetNamedSubStringValue("物料名称", tb2.Rows[0]["物料名称"].ToString());
                        bfmt.SetNamedSubStringValue("物料编码", tb2.Rows[0]["物料编号"].ToString());
                        bfmt.SetNamedSubStringValue("物料规格", tb2.Rows[0]["物料规格"].ToString());
                        bfmt.SetNamedSubStringValue("计量单位", tb2.Rows[0]["计量单位名称"].ToString());
                        bfmt.SetNamedSubStringValue("版本代号", 生产订单行数);
                        bfmt.SetNamedSubStringValue("时间", tb2.Rows[0]["生产订单行号"].ToString());
                        bfmt.SetNamedSubStringValue("行备注", 行备注);
                        bfmt.SetNamedSubStringValue("客户编码", 客户);
                        bfmt.SetNamedSubStringValue("业务员", 业务员);
                        string 原料编号;
                        string 原料规格;
                        string 原料名称;
                        string 存货代码;
                        //原料编号 = "";

                        二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString();
                        bfmt.SetNamedSubStringValue("二维", 二维码);

                        ///清空
                        for (int q = 1; q <= 10; q++)
                        {
                            bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("工序编号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("工序名称" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("是否倒冲" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("是否委外" + (q).ToString(), "");

                        }
                        总行数 = tb2.Rows.Count;
                        if (总行数 % 每页行数 == 0)
                        {
                            总页数 = 总行数 / 每页行数;
                        }
                        else
                        {
                            总页数 = 总行数 / 每页行数 + 1;
                        }
                        当前行 = 1;
                        for (int j = 0; j < tb2.Rows.Count; j++)
                        {
                            if (当前行 < 每页行数)
                            {
                                二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString() + "$" + tb2.Rows[j]["加工顺序"].ToString() + "$" + tb2.Rows[j]["工序编号"].ToString();
                                bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                                bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), tb2.Rows[j]["加工顺序"].ToString());
                                bfmt.SetNamedSubStringValue("工序编号" + (当前行).ToString(), tb2.Rows[j]["工序编号"].ToString());
                                bfmt.SetNamedSubStringValue("工序名称" + (当前行).ToString(), tb2.Rows[j]["工序名称"].ToString());
                                bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb2.Rows[j]["数量"].ToString());
                                bfmt.SetNamedSubStringValue("是否倒冲" + (当前行).ToString(), tb2.Rows[j]["是否倒冲"].ToString());
                                bfmt.SetNamedSubStringValue("是否委外" + (当前行).ToString(), tb2.Rows[j]["是否检验"].ToString());
                                当前行++;
                                sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员,生产订单行)";
                                sql += " values('生产流转卡工序表','" + tb2.Rows[0]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "','" + tb2.Rows[0]["生产订单行号"].ToString() + "')";
                                pub.ExcuteSql(sql, "生产流转卡工序表");
                                塞入派工记录(生产订单号ID, tb2.Rows[j]["生产订单行ID"].ToString(), tb2.Rows[0]["生产订单号"].ToString(), tb2.Rows[0]["生产订单行号"].ToString(), tb2.Rows[j]["工序编号ID"].ToString(), tb2.Rows[j]["加工顺序"].ToString(), 二维码, 工艺路线ID);
                            }
                            else
                            {
                                二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString() + "$" + tb2.Rows[j]["加工顺序"].ToString() + "$" + tb2.Rows[j]["工序编号"].ToString();
                                bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                                bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), tb2.Rows[j]["加工顺序"].ToString());
                                bfmt.SetNamedSubStringValue("工序编号" + (当前行).ToString(), tb2.Rows[j]["工序编号"].ToString());
                                bfmt.SetNamedSubStringValue("工序名称" + (当前行).ToString(), tb2.Rows[j]["工序名称"].ToString());                               
                                bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb2.Rows[i]["数量"].ToString());
                                bfmt.SetNamedSubStringValue("是否倒冲" + (当前行).ToString(), tb2.Rows[j]["是否倒冲"].ToString());
                                bfmt.SetNamedSubStringValue("是否委外" + (当前行).ToString(), tb2.Rows[j]["是否检验"].ToString());
                                bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                                塞入派工记录(生产订单号ID, tb2.Rows[j]["生产订单行ID"].ToString(), tb2.Rows[0]["生产订单号"].ToString(), tb2.Rows[0]["生产订单行号"].ToString(), tb2.Rows[j]["工序编号ID"].ToString(), tb2.Rows[j]["加工顺序"].ToString(), 二维码, 工艺路线ID);


                                for (int p = 1; p <= pages; p++)
                                {
                                    bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                                    bfmt.PrintOut();
                                }
                                ///清空
                                for (int q = 1; q <= 10; q++)
                                {
                                    bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("工序编号" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("工序名称" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("是否倒冲" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("是否委外" + (q).ToString(), "");

                                }
                                当前页码++;
                                当前行 = 1;
                                sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员,生产订单行)";
                                sql += " values('生产流转卡工序表','" + tb2.Rows[0]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "','" + tb2.Rows[0]["生产订单行号"].ToString() + "')";
                                pub.ExcuteSql(sql, "生产流转卡工序表");

                            }
                        }
                    }
                    if (当前页码 == 总页数)
                    {
                        bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                        //擦尾部,清空

                        for (int p = 1; p <= pages; p++)
                        {
                            bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                            bfmt.PrintOut();
                        }
                    }


                }
                bfmt.Close();
                //记录打印记录

                ba.Quit();
                pub.kill(ba);

            }
            return result;
        }

        private bool PrintRunCardDetail(string 生产订单号ID, string 生产订单行号, int pages)
        {
            ///
            int 每页行数 = 10;
            int 总页数 = 1;
            int 当前页码 = 1;
            int 当前行 = 1;
            int 总行数 = 0;
            string 二维码;
            bool result = false;
            BarTender.Application ba = new BarTender.Application();
            BarTender.Format bfmt;
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\工序流转卡两列.btw");

            string sql;
            sql = @" SELECT  distinct 工艺路线ID,生产订单号,生产订单行号 FROM      View_工序明细 where 生产订单号ID='" + 生产订单号ID + "' and 生产订单行号='" + 生产订单行号 + "' order by 工艺路线ID,生产订单行号  ";
            DataTable tb1 = pub.U8GetTable(sql);
            //选择工艺路线
            if (tb1 != null && tb1.Rows.Count > 1)
            {
                //所有的工艺路线ID 以逗号分隔
                List<string> 工艺路线IDs = new List<string>();
                for (int i = 0; i < tb1.Rows.Count; i++)
                {
                    工艺路线IDs.Add(tb1.Rows[i]["工艺路线ID"].ToString());
                }
                Form选择工艺路线 formSelect = new Form选择工艺路线();
                formSelect.PRoutingIds = string.Join("','", 工艺路线IDs);
                formSelect.ShowDialog();
                if (formSelect.DialogResult == DialogResult.OK)
                {
                    //从tb1找出工艺路线id对应的行并赋值给一个同样的table
                    tb1 = tb1.Select("工艺路线ID='" + formSelect.工艺路线ID + "'").CopyToDataTable();
                }
                else
                {
                    return false;
                }
            }
            ///总共有多少条工艺路线。
            if (tb1 != null)
            {
                for (int i = 0; i < tb1.Rows.Count; i++)
                {
                    删除工单(tb1.Rows[0]["生产订单号"].ToString(), tb1.Rows[i]["生产订单行号"].ToString());

                    string sql行备注 = $@"select Remark from  mom_orderdetail where MoId = {生产订单号ID} and SortSeq = '{生产订单行号}'";
                    string 行备注 = SqlSugarHelp.scope.Ado.GetString(sql行备注);

                    string sqlcus = $@"select Define2 from mom_order where MoId = {生产订单号ID}";
                    string 客户 = SqlSugarHelp.scope.Ado.GetString(sqlcus);

                    string sql业务员 = $@"select Define3 from mom_order where MoId = {生产订单号ID}";
                    string 业务员 = SqlSugarHelp.scope.Ado.GetString(sql业务员);
                    //取 行号 前面的
                    int index = 行备注.IndexOf("行号");
                    if (index != -1)
                    {
                        行备注 = 行备注.Substring(0, index-2);
                    }
                   
                    result = true;
                    sql = @" SELECT   生产订单号, 生产订单号ID, 工艺路线ID, 版本代码, 数量, 物料名称, 物料规格, 物料编号, 加工顺序, 工序名称, 
                工序编号ID, 计量单位名称, 计量单位编号, 工序编号, 生产订单行号, 制单日期,是否倒冲,是否委外,是否检验,生产订单行ID, cInvAddCode as 存货代码1
                FROM      View_工序明细 join Inventory
                    on Inventory.cinvcode=View_工序明细.物料编号 where 生产订单号ID='" + 生产订单号ID + "' and 生产订单行号 = '"+ tb1.Rows[i]["生产订单行号"].ToString() + "'  and 工艺路线ID='" + tb1.Rows[i]["工艺路线ID"].ToString() + "' order by 加工顺序 ";
                    DataTable tb2 = pub.U8GetTable(sql);
                    if (tb2 != null)
                    {
                        string sql生产订单行数 = $@"select count(1) from View_生产订单明细 where 生产订单号 = '{tb2.Rows[0]["生产订单号"].ToString()}'";
                        string 生产订单行数 = SqlSugarHelp.scope.Ado.GetString(sql生产订单行数);

                        string 工艺路线ID = tb2.Rows[0]["工艺路线ID"].ToString();


                        bfmt.SetNamedSubStringValue("生产订单号", tb2.Rows[0]["生产订单号"].ToString());
                        bfmt.SetNamedSubStringValue("物料名称", tb2.Rows[0]["物料名称"].ToString());
                        bfmt.SetNamedSubStringValue("物料编码", tb2.Rows[0]["物料编号"].ToString());
                        bfmt.SetNamedSubStringValue("物料规格", tb2.Rows[0]["物料规格"].ToString());
                        bfmt.SetNamedSubStringValue("计量单位", tb2.Rows[0]["计量单位名称"].ToString());
                        bfmt.SetNamedSubStringValue("版本代号", 生产订单行数);
                        bfmt.SetNamedSubStringValue("存货代码", tb2.Rows[0]["存货代码1"].ToString());
                        bfmt.SetNamedSubStringValue("时间", tb2.Rows[0]["生产订单行号"].ToString());
                        bfmt.SetNamedSubStringValue("行备注", 行备注);
                        bfmt.SetNamedSubStringValue("客户编码", 客户);
                        bfmt.SetNamedSubStringValue("业务员", 业务员);

                        string 原料编号;
                        string 原料规格;
                        string 原料名称;
                        string 存货代码;
                        Get原料信息(tb2.Rows[0]["生产订单行ID"].ToString(), tb2.Rows[0]["物料编号"].ToString());
                        //bfmt.SetNamedSubStringValue("原料编号", 原料编号);
                        //bfmt.SetNamedSubStringValue("原料名称", 原料名称);
                        //bfmt.SetNamedSubStringValue("原料规格", 原料规格);
                        //bfmt.SetNamedSubStringValue("存货代码", 存货代码);

                        二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString();
                        bfmt.SetNamedSubStringValue("二维", 二维码);

                        ///清空
                        for (int q = 1; q <= 10; q++)
                        {
                            bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("工序编号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("工序名称" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("是否倒冲" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("是否委外" + (q).ToString(), "");

                        }
                        总行数 = tb2.Rows.Count;
                        if (总行数 % 每页行数 == 0)
                        {
                            总页数 = 总行数 / 每页行数;
                        }
                        else
                        {
                            总页数 = 总行数 / 每页行数 + 1;
                        }
                        当前行 = 1;
                        for (int j = 0; j < tb2.Rows.Count; j++)
                        {
                            if (当前行 < 每页行数)
                            {
                                二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString() + "$" + tb2.Rows[j]["加工顺序"].ToString() + "$" + tb2.Rows[j]["工序编号"].ToString();
                                bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                                bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), tb2.Rows[j]["加工顺序"].ToString());
                                bfmt.SetNamedSubStringValue("工序编号" + (当前行).ToString(), tb2.Rows[j]["工序编号"].ToString());
                                bfmt.SetNamedSubStringValue("工序名称" + (当前行).ToString(), tb2.Rows[j]["工序名称"].ToString());
                                bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb2.Rows[j]["数量"].ToString());
                                bfmt.SetNamedSubStringValue("是否倒冲" + (当前行).ToString(), tb2.Rows[j]["是否倒冲"].ToString());
                                bfmt.SetNamedSubStringValue("是否委外" + (当前行).ToString(), tb2.Rows[j]["是否检验"].ToString());
                                当前行++;

                                sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员,生产订单行)";
                                sql += " values('生产流转卡工序表','" + tb2.Rows[0]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "','" + tb2.Rows[0]["生产订单行号"].ToString() + "')";
                                pub.ExcuteSql(sql, "生产流转卡工序表");
                                塞入派工记录(生产订单号ID, tb2.Rows[j]["生产订单行ID"].ToString(), tb2.Rows[0]["生产订单号"].ToString(), tb2.Rows[j]["生产订单行号"].ToString(), tb2.Rows[j]["工序编号ID"].ToString(), tb2.Rows[j]["加工顺序"].ToString(), 二维码, 工艺路线ID);

                            }
                            else
                            {
                                二维码 = tb2.Rows[0]["生产订单号"].ToString() + "$" + tb2.Rows[0]["生产订单行号"].ToString() + "$" + tb2.Rows[0]["物料编号"].ToString() + "$" + tb2.Rows[0]["数量"].ToString() + "$" + tb2.Rows[j]["加工顺序"].ToString() + "$" + tb2.Rows[j]["工序编号"].ToString();
                                bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                                bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), tb2.Rows[j]["加工顺序"].ToString());
                                bfmt.SetNamedSubStringValue("工序编号" + (当前行).ToString(), tb2.Rows[j]["工序编号"].ToString());
                                bfmt.SetNamedSubStringValue("工序名称" + (当前行).ToString(), tb2.Rows[j]["工序名称"].ToString());
                                bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb2.Rows[i]["数量"].ToString());
                                bfmt.SetNamedSubStringValue("是否倒冲" + (当前行).ToString(), tb2.Rows[j]["是否倒冲"].ToString());
                                bfmt.SetNamedSubStringValue("是否委外" + (当前行).ToString(), tb2.Rows[j]["是否检验"].ToString());
                                bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                                塞入派工记录(生产订单号ID, tb2.Rows[j]["生产订单行ID"].ToString(), tb2.Rows[0]["生产订单号"].ToString(), tb2.Rows[j]["生产订单行号"].ToString(), tb2.Rows[j]["工序编号ID"].ToString(), tb2.Rows[j]["加工顺序"].ToString(), 二维码, 工艺路线ID);

                                for (int p = 1; p <= pages; p++)
                                {
                                    bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                                    bfmt.PrintOut();
                                }
                                ///清空
                                for (int q = 1; q <= 10; q++)
                                {
                                    bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("工序编号" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("工序名称" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("是否倒冲" + (q).ToString(), "");
                                    bfmt.SetNamedSubStringValue("是否委外" + (q).ToString(), "");

                                }
                                当前页码++;
                                当前行 = 1;

                                sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员,生产订单行)";
                                sql += " values('生产流转卡工序表','" + tb2.Rows[0]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "','" + tb2.Rows[0]["生产订单行号"].ToString() + "')";
                                pub.ExcuteSql(sql, "生产流转卡工序表");

                            }
                        }
                    }
                    if (当前页码 == 总页数)
                    {
                        bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                        //擦尾部,清空

                        for (int p = 1; p <= pages; p++)
                        {
                            bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                            bfmt.PrintOut();
                        }
                    }
                    //sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员)";
                    //sql += " values('生产流转卡工序表','" + tb1.Rows[i]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "')";
                    //pub.ExcuteSql(sql, "生产流转卡工序表");

                }
                bfmt.Close();
                //记录打印记录

                ba.Quit();
                pub.kill(ba);

            }
            return result;
        }

        private void Get原料信息(string 生产订单行ID, string 物料编号)
        {
            string sql = " select bomid from v_bom_parent_rpt where invcode='" + 物料编号 + "' ";
            DataTable tb = pub.U8GetTable(sql);
            if (tb.Rows.Count > 0)
            {
                string bomid = tb.Rows[0][0].ToString();
                string sqlzj = "select cinvname 存货名称,cInvCode 存货编码,cinvaddcode 存货代码 from v_bom_opcomponent_rpt join inventory on inventory.cinvcode=v_bom_opcomponent_rpt.invcode  where bomid='" + bomid + "' and cdefwarehouse !='0402' ";
                DataTable tbzj = pub.U8GetTable(sqlzj);
                if (tbzj.Rows.Count > 0)
                {
                    for (int i = 0; i < tbzj.Rows.Count; i++)
                    {
                        string sqlzjb = "insert into Z_子件临时表 (存货名称,存货编码,存货代码,状态,登陆人员) values ('" + tbzj.Rows[i][0].ToString() + "','" + tbzj.Rows[i][1].ToString() + "','" + tbzj.Rows[i][2].ToString() + "','0','" + pub.LoginUser + "')";
                        pub.ExcuteSql(sqlzjb, "塞入子件临时表");
                        string sqlgxzt = "update Z_子件临时表 set 状态='1' where 存货编码 ='" + 物料编号 + "' and 登陆人员='" + pub.LoginUser + "'";
                        pub.ExcuteSql(sqlzjb, "更新子件临时表状态");

                        Get原料信息(生产订单行ID, tbzj.Rows[i]["存货编码"].ToString());
                   
                    }
                }
            }
        }
        /// <summary>
        /// 打印汇总流转卡
        /// </summary>
        /// <param name="生产订单号ID"></param>
        /// <param name="pages"></param>
        private void PrintRunCard(string 生产订单号ID, int pages)
        {
            ///
            int 每页行数 = 10;
            int 总页数 = 1;
            int 当前页码 = 1;
            int 当前行 = 1;
            int 总行数 = 0;
            string 二维码;
            BarTender.Application ba = new BarTender.Application();
            BarTender.Format bfmt;
            bfmt = ba.Formats.Open(System.Windows.Forms.Application.StartupPath + "\\汇总流转卡横向.btw");


            //try
            //{
            string sql;
            sql = @" Select cInvAddCode, 物料名称, 物料规格, 物料编号, 型材号, 长度, 材质, 客户编码, 米重, 硬度要求, 行号, MoId, 数量,
                生产订单号, 制单日期, 制单人,开工日期,完工日期 FROM      View_生产订单明细  where MoId='" + 生产订单号ID + "' order by 行号";
            DataTable tb = pub.U8GetTable(sql);
            if (tb != null)
            {
                总行数 = tb.Rows.Count;
                if (总行数 % 每页行数 == 0)
                {
                    总页数 = 总行数 / 每页行数;
                }
                else
                {
                    总页数 = 总行数 / 每页行数 + 1;
                }
                bfmt.SetNamedSubStringValue("生产订单号", tb.Rows[0]["生产订单号"].ToString());

                ///清空
                for (int q = 1; q <= 10; q++)
                {
                    bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("物料编号" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("物料名称" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("物料规格" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("型材号" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("长度" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("材质" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("开工日期" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("完工日期" + (q).ToString(), "");
                    bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                }
                bfmt.SetNamedSubStringValue("制单日期", tb.Rows[0]["制单日期"].ToString());


                for (int i = 0; i < 总行数; i++)
                {
                    if (当前行 < 每页行数)
                    {
                        二维码 = tb.Rows[i]["生产订单号"].ToString() + "$" + tb.Rows[i]["行号"].ToString() + "$" + tb.Rows[i]["物料编号"].ToString() + "$" + tb.Rows[i]["数量"].ToString();
                        bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                        bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), (i + 1).ToString());
                        bfmt.SetNamedSubStringValue("物料编号" + (当前行).ToString(), tb.Rows[i]["物料编号"].ToString());
                        bfmt.SetNamedSubStringValue("物料名称" + (当前行).ToString(), tb.Rows[i]["物料名称"].ToString());
                        bfmt.SetNamedSubStringValue("物料规格" + (当前行).ToString(), tb.Rows[i]["物料规格"].ToString());
                        bfmt.SetNamedSubStringValue("型材号" + (当前行).ToString(), tb.Rows[i]["型材号"].ToString());
                        bfmt.SetNamedSubStringValue("长度" + (当前行).ToString(), tb.Rows[i]["长度"].ToString());
                        bfmt.SetNamedSubStringValue("材质" + (当前行).ToString(), tb.Rows[i]["材质"].ToString());
                        bfmt.SetNamedSubStringValue("开工日期" + (当前行).ToString(), tb.Rows[i]["开工日期"].ToString());
                        bfmt.SetNamedSubStringValue("完工日期" + (当前行).ToString(), tb.Rows[i]["完工日期"].ToString());
                        bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb.Rows[i]["数量"].ToString());
                        
                        当前行++;
                    }
                    else
                    {
                        二维码 = tb.Rows[i]["生产订单号"].ToString() + "$" + tb.Rows[i]["行号"].ToString() + "$" + tb.Rows[i]["物料编号"].ToString() + "$" + tb.Rows[i]["数量"].ToString();
                        bfmt.SetNamedSubStringValue("二维" + (当前行).ToString(), 二维码);
                        bfmt.SetNamedSubStringValue("行号" + (当前行).ToString(), (i + 1).ToString());
                        bfmt.SetNamedSubStringValue("物料编号" + (当前行).ToString(), tb.Rows[i]["物料编号"].ToString());
                        bfmt.SetNamedSubStringValue("物料名称" + (当前行).ToString(), tb.Rows[i]["物料名称"].ToString());
                        bfmt.SetNamedSubStringValue("物料规格" + (当前行).ToString(), tb.Rows[i]["物料规格"].ToString());
                        bfmt.SetNamedSubStringValue("型材号" + (当前行).ToString(), tb.Rows[i]["型材号"].ToString());
                        bfmt.SetNamedSubStringValue("长度" + (当前行).ToString(), tb.Rows[i]["长度"].ToString());
                        bfmt.SetNamedSubStringValue("材质" + (当前行).ToString(), tb.Rows[i]["材质"].ToString());
                        bfmt.SetNamedSubStringValue("开工日期" + (当前行).ToString(), tb.Rows[i]["开工日期"].ToString());
                        bfmt.SetNamedSubStringValue("完工日期" + (当前行).ToString(), tb.Rows[i]["完工日期"].ToString());
                        bfmt.SetNamedSubStringValue("数量" + (当前行).ToString(), tb.Rows[i]["数量"].ToString());

                        bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                        for (int p = 1; p <= pages; p++)
                        {
                            bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                            bfmt.PrintOut();
                        }
                        ///清空
                        for (int q = 1; q <= 10; q++)
                        {
                            bfmt.SetNamedSubStringValue("二维" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("行号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("物料编号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("物料名称" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("物料规格" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("型材号" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("长度" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("材质" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("开工日期" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("完工日期" + (q).ToString(), "");
                            bfmt.SetNamedSubStringValue("数量" + (q).ToString(), "");
                        }
                        当前页码++;
                        当前行 = 1;
                    }
                }
            }

            if (当前页码 == 总页数)
            {
                bfmt.SetNamedSubStringValue("第几页共几页", "第:" + 当前页码.ToString() + "页,共:" + 总页数.ToString() + "页");
                //擦尾部,清空

                for (int p = 1; p <= pages; p++)
                {
                    bfmt.PrintSetup.Printer = this.printDialog1.PrinterSettings.PrinterName;
                    bfmt.PrintOut();
                }
            }
            bfmt.Close();
            //记录打印记录
            sql = "insert into Z_printrecord (打印类型,单据编号,打印时间,打印人员)";
            sql += " values('生产流转卡汇总表','" + tb.Rows[0]["生产订单号"].ToString() + "',getdate(),'" + pub.LoginUser + "')";
            pub.ExcuteSql(sql, "塞入打印流转卡记录表");


            ba.Quit();
            pub.kill(ba);

            //}
            //catch
            //{

            //}

        }

        private DataTable ListDetail(string 生产订单号ID)
        {
            string sql;
            sql = @"SELECT   MoId 生产订单号ID ,cInvAddCode, 物料名称, 物料规格, 物料编号, 型材号, 长度, 材质, 客户编码, 米重,数量, 硬度要求, 行号,开工日期,完工日期
                    FROM      View_生产订单明细 where MoId='" + 生产订单号ID + "' order by 行号 desc";

            //显示有无工艺路线
            sql = $@"SELECT 
                        p.MoId AS 生产订单号ID,    p.cInvAddCode,    p.物料名称,    p.物料规格,    p.物料编号,
                        p.型材号,    p.长度,    p.材质,    p.客户编码,    p.米重,    p.数量,    p.硬度要求,
                        p.行号,    p.开工日期,    p.完工日期,
                        CASE 
                            WHEN t.invcode IS NOT NULL THEN '有工艺路线'
                            ELSE '无工艺路线'
                        END AS 工艺路线状态
                    FROM 
                        View_生产订单明细 AS p
                    LEFT JOIN 
                        View_已审核有工艺路线 AS t ON p.物料编号 = t.invcode 
                    where MoId='{生产订单号ID}' order by 行号 desc";
            DataTable dtb = pub.U8GetTable(sql);
            return dtb;
        }

        private void dataGridView2_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            int pages;
            if (dataGridView2.Columns[e.ColumnIndex].Name == "D打印行")
            {
                if (this.printDialog1.ShowDialog(this) == DialogResult.OK)
                {
                    pages = this.printDialog1.PrinterSettings.Copies;

                    string 生产订单号ID = this.dataGridView2.Rows[e.RowIndex].Cells["D生产订单号ID"].Value.ToString();
                    string 行号 = this.dataGridView2.Rows[e.RowIndex].Cells["D行号"].Value.ToString();
                    //PrintRunCard(生产订单号ID,行号, pages);
                    if (PrintRunCardDetail(生产订单号ID, 行号, pages))
                    {
                        MessageBox.Show("打印完成", "提示", MessageBoxButtons.OK);
                    }
                    else
                    {
                    }
                }
            }
        }

        private void 删除工单(string 订单号, string 订单行号)
        {
            string sql已打印 = $"select 订单号 from Z_派工 where 订单号 = '{订单号}' and 订单行号 = '{订单行号}'";
            DataTable dt = pub.SysGetTable(sql已打印);
            if (dt != null && dt.Rows.Count > 0)
            {
                MessageBox.Show($"订单[{订单号}]，行号[{订单行号}]已经打印，请手撕掉老的纸质工单");
            }

            string sql删除工单 = $"delete from Z_派工 where  订单号 = '{订单号}' and 订单行号 = '{订单行号}'";
            pub.ExcuteSql(sql删除工单, "删工单");
        }

        private int 塞入派工记录(string 订单id,string 订单行id, string 订单号, string 订单行号,string 工序编号ID,string 加工顺序, string 工票码,string 工艺路线ID)
        {

            string sql = $@"insert into Z_派工( [订单号], [订单id], [订单行id], [订单行号], [产品编号], [产品名称], [订单数量], [工序OperationId], [加工顺序], [工序编号], [工序名称], [工时],  [工票码],  [状态], [派工人], [发料人], [发料状态], [是否检验],派工日期,工序单价)
                            select [生产订单号], [生产订单号ID],[生产订单行ID],[生产订单行号],[物料编号], [物料名称],[数量],	[工序编号ID]	,[加工顺序], [工序编号],  [工序名称], [标准工序标准工时],'{工票码}','已发料','{pub.LoginUser}','{pub.LoginUser}', '已发料',[是否检验],'{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}',工序单价
                            from View_工序明细 where 生产订单号 = '{订单号}' and 生产订单行号 = '{订单行号}' and 工序编号ID = '{工序编号ID}' 
                        and 加工顺序 = '{加工顺序}' and 工艺路线ID = '{工艺路线ID}' and not exists ( select 生产订单号ID from Z_派工 where Z_派工.订单号 = 生产订单号 and Z_派工.订单行号 = 生产订单行号  and Z_派工.加工顺序 = View_工序明细.加工顺序)";
            int r = pub.ExcuteSql(sql, "塞入派工记录");
            return r;
        }
        
    }
}
